<?php
/*
 module:		地区分类表
 create_time:	2024-03-11 10:24:03
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\model\Categorydistrict as CategorydistrictModel;

class Categorydistrict extends Common
{

    /*start*/
    /**
     * @api {get} /Categorydistrict/index 01、首页数据列表
     */
    function index()
    {


        $parentid = $this->request->post('parentid', 0, 'intval');
        $type = $this->request->post('type', 1, 'intval');//1-获取下级；2-三级联动
        $where = [];
        $field = 'id,parentid,categoryname';
        $orderby = 'id asc';
        if ($type == 1) {
            $res = CategorydistrictModel::where("parentid", $parentid)->field($field)->order($orderby)->select()->toArray();
        } else {
            $res = CategorydistrictModel::where("parentid", 0)->field($field)->order($orderby)->select()->toArray();
            $list2 = CategorydistrictModel::where("parentid", ">", 0)->field($field)->order($orderby)->select()->toArray();
             foreach ($res as &$li){
                 $children =[];
                 foreach ($list2 as $key1=>$li2){
                    if($li2["parentid"]==$li["id"]){
                        $vas=$li2;
                        $children2 =[];
                        foreach ($list2 as $key2=>$li3){
                            if($li3["parentid"]==$vas["id"]){
                                $children2[]=$li3;
                              unset($list2[$key2]);
                            }
                        }
                        $vas["children"]=$children2;
                        $children[]=$vas;
                        unset($list2[$key1]);
                    }
                 }
                 $li["children"]=$children;
             }
            unset($children,$children2);
        }
        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }
    /*end*/

}

